﻿import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Dialogs 1.2
import Union.hw.Systimer 1.0
import timer 1.0

ApplicationWindow {
    id: mainwindow
    visible: true
    width: 1600
    height: 900
    title: qsTr("AutoTest")
    property int grid_scale: 5          // grid view 缩放系数
    property int devOnlinenum: 0
    property string devName: "网安板"
    property int stat_stoped    : 5
    property int stat_disabled  : 4

    /* menu菜单栏 */
    menuBar: MenuBar {
        Menu {
            title: "文件"
            MenuItem {
                text: "加载配置文件"
                onClicked: {
                    if(false == valuesource.startTesting){
                        fileSelect.open()
                    }else{
                        messageShow.setText("请先停止老化，再更改测试脚本！")
                        messageShow.open()
                    }
                }
            }
            MenuItem {
                text: "退出"
                onClicked: {
                    Qt.quit()
                }
            }
        }
        Menu {
            title: "显示"
            MenuItem {
                text: "增加设备"
                onClicked: {
                    machineModel.addOne()
                }
            }
            MenuItem {
                text: "减少设备"
                onClicked: {
                    machineModel.removeLaterOne()
                }
            }
            MenuItem {
                text: "放大"
                onClicked: {
                    zoomGridview(true)
                }
            }
            MenuItem {
                text: "缩小"
                onClicked: {
                    zoomGridview(false)
                }
            }
        }
        Menu {
            title: "设置"
            MenuItem {
                text: "查看测试配置"
                onClicked: {
                    systimer.testCaseSetup()
                }
            }
            MenuItem {
                text: "更改设备名"
                onClicked: {
                    inputdialog.label = "请输入设备名："
                    inputdialog.hint  = "网安板"
                    inputdialog.open()
                }
            }
            MenuItem {
                text: "网络配置"
                onClicked: {
                    netconfigDialog.open()
                }
            }
        }

        Menu {
            title: "帮助"
            MenuItem {
                text: "技术支持"
                onClicked: {
                    messageShow.setText("联系邮箱: wushuai@hwsys.cn")
                    messageShow.open()
                }
            }
            MenuItem {
                text: "报告bug"
                onClicked: {
                    messageShow.setText("联系邮箱: wushuai@hwsys.cn")
                    messageShow.open()
                }
            }
            MenuItem {
                text: "关于"
                onClicked: {
                    messageShow.setText("基于Qt5 qml UDP报文、Centos7 qtbase运行环境，工控机系统压力与老化一体测试工具。\n 工控机产品信息：www.hwsys.cn")
                    messageShow.open()
                }
            }
        }
    }

    MachineModel{ id: machineModel }
    NetworkConfigForm{ id: netconfigDialog }

    Rectangle{
        id: rectangle_grid
        x:0 ; y:0
        width: parent.width
        height: parent.height*0.9

        GridView{
            id: gridview
            anchors.fill: parent
            cellWidth: mainwindow.width/5
            cellHeight: cellWidth
            model: machineModel
            clip: true;

            delegate: Item{
                id: dele_item
                width: gridview.cellWidth*0.9; height: width;
                MachineForm{
                    id: machine
                    width: dele_item.width
                    height: dele_item.width
                    name:   MdevName
                    devIndex: index
                    cpuRate: McpuRate
                    memRate: MmemRate
                    tempera: McpuTemp
                    videoTemp: MvdoTemp
                    hddTemp: MhddTemp
                    linesMask: MlinesMask
                    macString: Mmac
                    ipString: Mip
                    enabled:   Menabled
                    testcaseStr: MtcaseStr      // 测试实例字符串
                    statusLevel: MtstatLevel
                    statusString: MtstatStr     // 测试运行状态字符串
                    testTimes: Mttimes          // 测试时间
                    cmdTimes: Mcmdtimes         // 当前命令测试时间
                    cmdProcess: Mcmdprocess     // 当前命令测试进度
                    testProcess: Mtprocess      // 测试进度
                    statusRefresh: Mrefresh
                }
            }
        }

        Slider {
            id: slider_gridview
            anchors.right: parent.right
            anchors.rightMargin: -10
            y: 0
            height: parent.height
            rotation: 180
            stepSize: 1
            to: 99
            orientation: Qt.Vertical
            value: 0

            onValueChanged: {
                gridview.currentIndex = value
            }
        }
    }

    Rectangle{
        y: rectangle_grid.height
        x: 0
        width: parent.width
        height: parent.height - y

        Label {
            id: label_debug
            x: 563
            y: 17
            width: 90
            height: 43
            //text: qsTr("debug:")
            verticalAlignment: Text.AlignVCenter
        }

        RoundButton {
            id: roundButton
            x: 15
            y: 20
            text: "\u2191";
            onClicked: {
                if(gridview.currentIndex >= grid_scale){
                    gridview.currentIndex -= grid_scale
                    slider_gridview.value = gridview.currentIndex
                }
            }
        }

        RoundButton {
            id: roundButton1
            x: 70
            y: 20
            text: "\u2193"
            onClicked: {
                if(gridview.currentIndex < gridview.count){
                    gridview.currentIndex += grid_scale
                    slider_gridview.value = gridview.currentIndex
                }
            }
        }

        RoundButton {
            x: 289
            y: 20
            text: "+"
            onClicked: {
                zoomGridview(true)
            }
        }

        RoundButton {
            x: 344
            y: 20
            text: "-"
            onClicked: {
                zoomGridview(false)
            }
        }

        CheckBox {
            id: cpuRate
            x: 400
            y: 20
            text: "CPU占用率"
            checkState: "Checked"
            onCheckedChanged: {
                if(cpuRate.checked){
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask |= 0x01;
                    }
                }else{
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask &= 0x1E;
                    }
                }
            }
        }

        CheckBox {
            id: memRate
            x: cpuRate.x + 100
            y: 20
            text: "内存占用率"
            checkState: "Checked"
            onCheckedChanged: {
                if(memRate.checked){
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask |= 0x02;
                    }
                }else{
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask &= 0x1D;
                    }
                }
            }
        }

        CheckBox {
            id: cpuTemp
            x: memRate.x + 102
            y: 20
            text: "CPU温度"
            checkState: "Checked"
            onCheckedChanged: {
                if(cpuTemp.checked){
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask |= 0x04;
                    }
                }else{
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask &= 0x1B;
                    }
                }
            }
        }

        CheckBox {
            id: videoTemp
            x: cpuTemp.x + 90
            y: 20
            text: "显卡温度"
            checkState: "Unchecked"
            onCheckedChanged: {
                if(videoTemp.checked){
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask |= 0x08;
                    }
                }else{
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask &= 0x17;
                    }
                }
            }
        }

        CheckBox {
            id: hddTemp
            x: videoTemp.x + 90
            y: 20
            text: "硬盘温度"
            checkState: "Unchecked"
            onCheckedChanged: {
                if(hddTemp.checked){
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask |= 0x10;
                    }
                }else{
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).MlinesMask &= 0x0F;
                    }
                }
            }
        }

        Switch {
            id: switch_poweroffAuto
            x: 126
            y: 20
            text: "测试完成后关机"
            checked: true

            onCheckedChanged: {
                systimer.setPoweroffAuto(switch_poweroffAuto.checked)
            }
        }

        Button {
            id: button1_rebootAll
            anchors.right: button_poweroffAll.left
            anchors.rightMargin: 20
            y: 20
            text: qsTr("全部重启")
            onClicked: {
                systimer.turnOffAll(true)
            }
        }

        Button {
            id: button_poweroffAll
            anchors.right: button_startTest.left
            anchors.rightMargin: 20
            y: 20
            text: qsTr("全部关机")
            onClicked: {
                systimer.turnOffAll(false)
            }
        }

        Button {
            id: button_startTest
            anchors.right: parent.right
            anchors.rightMargin: 20
            y: 20
            text: qsTr("开始老化")
            onClicked: {
                if(false == valuesource.startTesting){
                    valuesource.startTesting = true
                    button_startTest.text = "停止老化"
                    systimer.autoTestSwitch(true, true)
                    for(var i=0; i<devOnlinenum; i++){
                        machineModel.get(i).Mttimes = 0
                    }
                    testTimer.start()
                }else{
                    valuesource.startTesting = false
                    button_startTest.text = "开始老化"
                    systimer.autoTestSwitch(false, true)
                    testTimer.stop()
                }
            }
        }

        ProgressBar{
            id: progress_total
            x:0
            value: 20
            from: 0
            to: 100
            width: parent.width
            anchors.bottom: parent.bottom
        }
    }

    Systimer{
        id: systimer
        /* 网络配置 */
        onNetworkConfig: {
            netconfigDialog.setCombox(localIP1, localIP2, localIP3)
            console.debug("ip1: " + localIP1 + ", ip2: " + localIP2 + ", ip3: " + localIP3)
            netconfigDialog.open()
        }
        /* 测试脚本配置 */
        onTestcaseConfig: {
            console.debug("refresh testcase, machineModel counts: " + machineModel.count)
            for(var dindex=0; dindex<machineModel.count; dindex++){
                machineModel.get(dindex).MtcaseStr = systimer.getDevTestCaseStr(dindex, 0, 0)
            }
        }
        /* 设备本机状态信息刷新 */
        onStatusTrigger: {
            if(devIndex < 0 || devIndex > 99){
                return;
            }

            machineModel.get(devIndex).Menabled  = true;
            machineModel.get(devIndex).McpuRate  = cpuRate
            machineModel.get(devIndex).MmemRate  = memRate
            machineModel.get(devIndex).McpuTemp  = cpuTemp
            machineModel.get(devIndex).MvdoTemp  = vdoTemp
            machineModel.get(devIndex).MhddTemp  = hddTemp
            machineModel.get(devIndex).Mmac      = macStr
            machineModel.get(devIndex).Mip       = ipStr
            machineModel.get(devIndex).Mrefresh  = (machineModel.get(devIndex).Mrefresh>0?0:1)

            if(devIndex+1 > devOnlinenum){
                devOnlinenum = devIndex+1
            }
            if(devOnlinenum >= machineModel.count){
                machineModel.addOne()
                if(devOnlinenum < 5){
                    if(grid_scale > devOnlinenum){
                        zoomGridview(true)
                    }
                }
            }
        }
        /* 设备测试状态刷新 */
        onTestDevTrigger: {
            if(devStatus === stat_stoped){
                machineModel.get(devIndex).Menabled = false;
            }else if(devStatus === stat_disabled){

            }else{
                machineModel.get(devIndex).MtcaseStr = systimer.getDevTestCaseStr(devIndex, cmdIndex, cmdRepeatIndex)
                if(cmdIndex === 0 && cmdRepeatIndex === 1){     // 设备开始运行测试
                    machineModel.get(devIndex).Mttimes   = 0;
                    machineModel.get(devIndex).Mtprocess = 0;
                }
            }
        }
        /* 设备测试进度刷新 */
        onTestProcessTrigger: {
            machineModel.get(devIndex).Mcmdtimes    = currentTime
            machineModel.get(devIndex).Mcmdprocess  = currentProcess
            machineModel.get(devIndex).Mtprocess    = totalProcess
        }

        /* 设备运行状态刷新 */
        onTestDevStatTrigger: {
            machineModel.get(devIndex).MtstatLevel = level
            machineModel.get(devIndex).MtstatStr = statusStr
        }
        /* 测试结束 */
        onTestAllEned: {
            valuesource.startTesting = false
            button_startTest.text = "开始老化"
            testTimer.stop()
        }
        /* ping测试反馈 */
        onPingTestRet: {
            if(logTxt.length > 0){
                netconfigDialog.appendPingTestLog(logTxt)
            }else
                netconfigDialog.clearPingTestLog();
        }
    }

    function zoomGridview(isEnlarge){
        if(isEnlarge){
            if(grid_scale > 1)
                grid_scale--
            gridview.cellWidth  = mainwindow.width/grid_scale;
            gridview.cellHeight = gridview.cellWidth
        }else{
            if(grid_scale < 8)
                grid_scale++
            gridview.cellWidth  = mainwindow.width/grid_scale;
            gridview.cellHeight = gridview.cellWidth
        }
    }

    /* 消息提示对话框 */
    MessageDialog{
        id: messageShow
        icon: StandardIcon.Information
        title: "提示"
        modality: Qt.WindowModal
        text: "none..."
    }

    /* 输入对话框 */
    InputDialog{
        id: inputdialog
        visible: false

        onAccepted: {
            devName = inputdialog.editText.text
            for(var i=0; i<machineModel.count; i++){
                machineModel.get(i).MdevName = devName+"-"+(i+1)
            }
        }
    }

    /* 测试脚本配置 对话框 */
    FileDialog{
        id: fileSelect
        title: "选择测试脚本文件"
        selectExisting: true
        selectFolder: false
        selectMultiple: false
        // fileUrl: "file:./settings/4小时-基本测试.xml"
        // folder: "file:./settings/"
        nameFilters: ["xml文件(*.xml)"]


        onAccepted: {
            valuesource.testFileName = fileSelect.fileUrl;
            systimer.changeTestCase(valuesource.testFileName);
            console.log("you select: " + valuesource.testFileName)
        }
    }

    ValueSource{
        id: valuesource
    }

    Timer {
        id: testTimer
        interval: 1000
        onTimeout:{
            for(var i=0; i<devOnlinenum; i++){
                machineModel.get(i).Mttimes = machineModel.get(i).Mttimes+1
            }
        }
    }
}
